package com.dmy.ui;

import com.dmy.config.GameConfig;

import javax.swing.*;
import java.awt.*;
import java.util.Objects;
import java.util.Random;


/**
 * 坦克抽象类
 *  用于对公共的方法进行封装
 */
public abstract class Tank {

    protected static final int speed = 10; // 移动速度
    private Bullet bullet; // 子弹
    private String name; // 坦克名称
    private Color color; // 坦克颜色
    private int x; // x轴
    private int y; // y轴
    private int direction = 1; // 方向： 1 为上 ，-1 为下 ，2 为左 -2 为右
    private boolean isEnemyTank = true; // 是否是敌军坦克 默认是true

    private int lifeNum ; // 默认的生命数

    // 坦克死亡
    public final void over(){
        if(lifeNum>0){
            lifeNum--;
        }
        if(isEnemyTank()&&!this.getName().equals("钛")){
            this.setName(lifeNum+"");
        }
    }

    // 坦克添加生命 敌军坦克不允许添加
    public void addLife() {
        if(isEnemyTank())
            return;
        lifeNum++;
    }

    /**
     * 获取生命数
     * @return
     */
    public int getLifeNum() {

        return lifeNum;
    }

    // 是否敌军坦克
    public boolean isEnemyTank() {
        return isEnemyTank;
    }

    public void setEnemyTank(boolean enemyTank) {
        isEnemyTank = enemyTank;
    }



    public Tank(int x, int y,int life) {
        this.x = x;
        this.y = y;
        bullet = new Bullet();
        if(isEnemyTank()){
            life = (life = new Random().nextInt(10))==3||life==7?10:new Random().nextInt(2)+1;
        }
        this.setName(String.format(life==10?"钛":life+""));
        this.lifeNum = life;
    }

    public final String getName() {
        return name;
    }

    public final void setName(String name) {
        this.name = name;
    }

    public final Color getColor() {
        return color;
    }

    public final void setColor(Color color) {
        this.color = color;
    }

    public final int getX() {
        return x;
    }

    public final void setX(int x) {
        this.x = x;
    }

    public final  int getY() {
        return y;
    }

    public final void setY(int y) {
        this.y = y;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Tank tank = (Tank) o;
        return x == tank.x &&
                y == tank.y;
    }

    @Override
    public int hashCode() {
        return Objects.hash(x, y);
    }
    public final void moveUp(JComponent jComponent){
        this.direction = 1;
        if( this.y - speed<=0)
            return;
        this.y -= speed;
        jComponent.repaint();
    }
    public final void moveDown(JComponent jComponent){
        this.direction = -1;
        if( this.y + speed>= GameConfig.WINDOW_HEIGHT -55)
            return;
        this.y += speed;
        jComponent.repaint();
    }
    public final void moveLeft(JComponent jComponent){
        this.direction = 2;
        if( this.x - speed<0)
            return;
        this.x -= speed;
        jComponent.repaint();
    }
    public final void moveRight(JComponent jComponent){
        this.direction = -2;
        if( this.x + speed>=GameConfig.WINDOW_WIDTH-60)
            return;
        this.x += speed;
        jComponent.repaint();
    }


    // 画坦克
    public final void paint(Graphics g) {
        Graphics2D  gg = (Graphics2D )g;
        switch (direction){
            case 1:
                drawTankForUp(gg);
                break;
            case -1:
                drawTankForDown(gg);
                break;
            case 2:
                drawTankForLeft(gg);
                break;
            case -2:
                drawTankForRight(gg);
                break;

        }
        bullet.afterInvoke(gg);
    }

    // 发射炮弹
    public final void attack(JComponent jComponent){
        bullet.attack(jComponent,this.getColor(),getBulletX(direction),getBulletY(direction),direction,this);
    }

    private int getBulletY(int direction) {
        int defaultV = 0;
        switch (direction){
            case 1:
                defaultV = this.getY()-20;
                break;
            case -1:
                defaultV = this.getY()+55;
                break;
            case 2:
                defaultV = this.getY()+17;
                break;
            case -2:
                defaultV = this.getY()+17;
                break;

        }
        return defaultV;
    }

    private int getBulletX(int direction){
        int defaultV = 0;
        switch (direction){
            case 1:
                defaultV = this.getX()+19;
                break;
            case -1:
                defaultV = this.getX()+19;
                break;
            case 2:
                defaultV = this.getX()-20;
                break;
            case -2:
                defaultV = this.getX()+55;
                break;

        }
        return defaultV;
    }

    private final void drawTankForUp(Graphics2D gg){
        gg.setColor(this.getColor());
        gg.fillRect(this.getX(),this.getY(),10,60);
        gg.fillRect(this.getX()+36,this.getY(),10,60);
        gg.fillRect(this.getX()+10,this.getY()+17,26,26);
        gg.setStroke(new BasicStroke(3.0F));
        gg.setColor(this.getColor());
        gg.drawLine(this.getX()+22,this.getY()+28,this.getX()+22,this.getY()-20);
        gg.setColor(Color.black);
        gg.fillArc(this.getX()+17,this.getY()+25,10,10,0,360);
        gg.setFont(new Font("黑体",Font.BOLD,12));
        gg.setColor(this.getColor());
        gg.drawString(this.getName(),this.getX()+17,this.getY()+58);
    }
    private final  void drawTankForDown(Graphics2D gg){
        gg.setColor(this.getColor());
        gg.fillRect(this.getX(),this.getY(),10,60);
        gg.fillRect(this.getX()+36,this.getY(),10,60);
        gg.fillRect(this.getX()+10,this.getY()+17,26,26);
        gg.setStroke(new BasicStroke(3.0F));
        gg.setColor(this.getColor());
        gg.drawLine(this.getX()+22,this.getY()+28,this.getX()+22,this.getY()+74);
        gg.setColor(Color.black);
        gg.fillArc(this.getX()+17,this.getY()+25,10,10,0,360);
        gg.setFont(new Font("黑体",Font.BOLD,12));
        gg.setColor(this.getColor());
        gg.drawString(this.getName(),this.getX()+17,this.getY()+10);
    };
    private  final void drawTankForLeft(Graphics2D gg){
        gg.setColor(this.getColor());
        gg.fillRect(this.getX(),this.getY(),60,10);
        gg.fillRect(this.getX(),this.getY()+36,60,10);
        gg.fillRect(this.getX()+17,this.getY()+10,26,26);
        gg.setStroke(new BasicStroke(3.0F));
        gg.setColor(this.getColor());
        gg.drawLine(this.getX()+28,this.getY()+22,this.getX()-20,this.getY()+22);
        gg.setColor(Color.black);
        gg.fillArc(this.getX()+25,this.getY()+17,10,10,0,360);
        gg.setFont(new Font("黑体",Font.BOLD,12));
        gg.setColor(this.getColor());
        gg.drawString(this.getName(),this.getX()+60-12,this.getY()+27);
    };
    private  final void drawTankForRight(Graphics2D gg){
        gg.setColor(this.getColor());
        gg.fillRect(this.getX(),this.getY(),60,10);
        gg.fillRect(this.getX(),this.getY()+36,60,10);
        gg.fillRect(this.getX()+17,this.getY()+10,26,26);
        gg.setStroke(new BasicStroke(3.0F));
        gg.setColor(this.getColor());
        gg.drawLine(this.getX()+28,this.getY()+22,this.getX()+74,this.getY()+22);
        gg.setColor(Color.black);
        gg.fillArc(this.getX()+25,this.getY()+17,10,10,0,360);
        gg.setFont(new Font("黑体",Font.BOLD,12));
        gg.setColor(this.getColor());
        gg.drawString(this.getName(),this.getX(),this.getY()+27);
    };

}
